package com.ddm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ddm.entity.Level1;
import com.ddm.entity.Level4;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 四级指标表 Mapper 接口
 * </p>
 *
 * @author 丁丁猫
 * @since 2025-01-13
 */
@Mapper
public interface Level4Mapper extends BaseMapper<Level4> {
    @Select("SELECT COUNT(*) FROM level4 WHERE num_id LIKE CONCAT(#{parentNumId}, '.%') AND standard_id = #{standardId}")
    int selectCountByParentNumId(@Param("parentNumId") String parentNumId, @Param("standardId") Long standardId);

    @Update("UPDATE level4 SET if_next_level = #{value} WHERE num_id = #{parentNumId} AND standard_id = #{standardId}")
    void updateIfNextLevel(@Param("parentNumId") String parentNumId, @Param("standardId") Long standardId, @Param("value") int value);
    @Update("UPDATE level4 SET num_id = REPLACE(num_id, #{oldPrefix}, #{newPrefix}) WHERE num_id LIKE CONCAT(#{oldPrefix}, '%') AND standard_id = #{standardId}")
    void updateNumIdPrefix(@Param("oldPrefix") String oldPrefix, @Param("newPrefix") String newPrefix, @Param("standardId") Long standardId);

    // 启用批量插入
    @Insert("<script>" +
            "INSERT INTO level4 (standard_id, num_id, level, if_next_level) " +
            "VALUES <foreach collection='list' item='item' separator=','>" +
            "(#{item.standardId}, #{item.numId}, #{item.level}, #{item.ifNextLevel})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("list") List<Level4> list);
}
